<template>
   <div>
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="角色名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="form.name" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item
        label="角色描述"
        :label-width="formLabelWidth"
        prop="description"
      >
        <el-input v-model="form.description" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submitHandle">确 定</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'VueAdminTemplateMasterRoleDig',
props:{
isEdit:{
  type:Boolean
},
dialogFormVisible:{
  type:Boolean
},
row:{
  type:Object,
  default:()=>{}
}
},
watch: {
  isEdit:{
handler(newVal){
  console.log(newVal,9);
  if(newVal){
    this,form=JSON.parse(JSON.stringify(this.row))
  }
},
immediate: true,
  }
},
  data() {
    return {
      form:{
        name:'',
        description:""
      },
      formLabelWidth:'120px',
      rules:{
        name:[
           { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        description:[
           { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }
    };
  },

  mounted() {
    
  },

  methods: {
    cancel() {
      this.$emit("update:dialogFormVisible", false);
    },
    submitHandle() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd();
        }
      });
    },
    async doAdd() {
      // 添加
      // 校验成功
      const res = await addRole(this.form);
      this.success();
    },
    async doEdit() {
      // 编辑
      await updateRole(this.form);
      this.success();
    },
    success() {
      // 成功后需要做的操作
      // 关闭对话框+刷新页面
      this.$emit("success");
    },
  
  },
};
</script>

<style lang="scss" scoped>

</style>